<?php

// +----------------------------------------------------------------------
// | Admin Plugin for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2025 ThinkAdmin [ thinkadmin.top ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/think-plugs-admin
// | github 代码仓库：https://github.com/zoujingli/think-plugs-admin
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\model\BannerDao;
use app\model\CateDao;
use app\model\CoachDao;
use app\model\RegionDao;
use Carbon\Carbon;
use think\admin\Controller;
use think\db\Query;

/**
 * 教练入驻
 * @class User
 * @package app\admin\controller
 */
class Coach extends Controller
{
    protected $table = '';

    protected function initialize()
    {
        $this->table = CoachDao::query();
    }


    /**
     * 教练入驻
     * @auth true
     * @menu true
     */
    public function index()
    {
        $this->title = '教练入驻管理';
        $this->_query($this->table)
            ->with(['city' => function (Query $query) {
                $query->field('id,code,name');
            }])->equal('status')
            ->like('name')
            ->order('id DESC')
            ->dateBetween('create_at')
            ->page();
    }

    protected function _page_filter(&$list)
    {
        foreach ($list as &$vo) {
            $arr = explode(',', $vo['lesson_item']);
            $cate = CateDao::whereIn('id', $arr)
                ->field('id,name')
                ->order('sort DESC,id ASC')
                ->select();
            $vo['cate'] = $cate;

            $birthday = Carbon::parse($vo['birthday']);
            $vo['age'] = (int)$birthday->diffInYears(Carbon::now());
            $vo['working_years'] = (Carbon::now())->year - $vo['working_year'];

            $arr = explode(',', $vo['lesson_district']);
            $area = RegionDao::whereIn('code', $arr)
                ->field('id,name')
                ->order('sort DESC,id ASC')
                ->select();
            $vo['area'] = $area;

        }
    }


    /**
     * 添加
     * @auth true
     */
    public function add()
    {

        $this->_form($this->table, 'form');
    }

    /**
     * 编辑系
     * @auth true
     */
    public function edit()
    {
        $this->title = '编辑';
        $this->_form($this->table, 'form');
    }


    protected function _form_filter(&$data)
    {
        if ($this->request->isGet()) {
            $list = CateDao::where('status', 1)
                ->field("id,pid,name")
                ->order("pid asc,sort desc,id asc")
                ->select();
            $tree = getTree($list->toArray());
            $city = null;
            if (!empty($data['lesson_city'])) {
                $city = RegionDao::field('code,parent_code,name')->where('code', $data['lesson_city'])->find();
            }
            $this->city = $city;
            $this->tree = $tree;
            if ($data['photo']) {
                $data['photo'] = str_replace(',', '|', $data['photo']);
            }
            if ($data['honor']) {
                $data['honor'] = str_replace(',', '|', $data['honor']);
            }
        } else {
            $lesson_item = $data['lesson_item'] ?? [];
            $data['lesson_item'] = implode(',', $lesson_item);
            $lesson_district = $data['lesson_district'] ?? [];
            $data['lesson_district'] = implode(',', $lesson_district);
            $lesson_room = $data['lesson_room'] ?? [];
            $data['lesson_room'] = implode(',', $lesson_room);
            $data['photo'] = str_replace('|', ',', $data['photo']);
            $data['honor'] = str_replace('|', ',', $data['honor']);
            //$this->error("f------",$data);
        }
    }

    /**
     * @auth true
     * @return void
     */
    public function region()
    {
        $pid = $this->request->param('parent_code', null);
        $list = RegionDao::where('parent_code', $pid ? $pid : null)
            ->field("id,code,name")
            ->order("pid asc,sort desc,id asc")
            ->select();
        $this->success('获取成功', $list);
    }


    protected function _form_result(&$result, &$data)
    {
        if ($result !== false) {
            $spm = $data['spm'] ?? 0;
            jumpUrl(1, '操作成功', '/admin.html#/admin/coach/index.html?&spm=' . $spm);
        }
    }

    /**
     * @auth true
     */
    public function remove()
    {
        $this->_applyFormToken();
        $this->_delete($this->table);
    }


    /**
     * 修改
     * @auth true
     */
    public function state()
    {
        //CoachDao::mSave($this->_vali([
        //    'status.in:0,1'  => '状态值范围异常！',
        //    'status.require' => '状态值不能为空！',
        //]));
        $this->_applyFormToken();
        $this->_save($this->table, $this->_vali([
            'id.require' => 'ID 不能为空！',
            'status.require' => '状态不能为空！',
            'status.in:1,2' => '状态值范围异常！'
        ]));

    }
}
